import type { Polygon, Geometry, LineString, GeoJsonProperties, Feature } from 'geojson'
// import turf from '@turf/turf'
// import { circle as turfCircle, sector as turfSector, lineArc as turfLineArc } from '@turf/turf'
import type { LngLat } from './lnglat'

import * as turf from '@turf/turf'

export function feature<G extends Geometry, P extends GeoJsonProperties>(
  geometry: G,
  properties: P
): Feature<G, P> {
  return {
    type: 'Feature',
    geometry,
    properties
  }
}

export function geometryOfCircle(center: LngLat, radius: number, steps: number = 20): Polygon {
  const f = turf.circle([center.lng, center.lat], radius / 1000.0, { steps, units: 'kilometers' })
  return f.geometry
}

export function featureOfCircle<P extends GeoJsonProperties>(
  center: LngLat,
  radius: number,
  properties: P,
  steps: number = 20
): Feature<Polygon, P> {
  return turf.circle([center.lng, center.lat], radius / 1000.0, {
    steps,
    units: 'kilometers',
    properties
  })
}

export function geometryOfArc(
  center: LngLat,
  radius: number,
  bearing1: number,
  bearing2: number,
  steps: number = 30
): LineString {
  const f = turf.lineArc([center.lng, center.lat], radius / 1000.0, bearing1, bearing2, {
    steps,
    units: 'kilometers'
  })
  return f.geometry
}
export function featureOfArc<P extends GeoJsonProperties>(
  center: LngLat,
  radius: number,
  bearing1: number,
  bearing2: number,
  properties: P,
  steps: number = 30
): Feature<LineString, P> {
  const ret = turf.lineArc([center.lng, center.lat], radius / 1000.0, bearing1, bearing2, {
    steps,
    units: 'kilometers'
  })
  ret.properties = properties
  return ret as Feature<LineString, P>
}

export function geometryOfSector(
  center: LngLat,
  radius: number,
  bearing1: number,
  bearing2: number,
  steps: number = 30
): Polygon {
  const f = turf.sector([center.lng, center.lat], radius / 1000.0, bearing1, bearing2, {
    steps,
    units: 'kilometers'
  })
  return f.geometry
}

export function featureOfSector<P extends GeoJsonProperties>(
  center: LngLat,
  radius: number,
  bearing1: number,
  bearing2: number,
  properties: P,
  steps: number = 30
): Feature<Polygon, P> {
  const ret = turf.sector([center.lng, center.lat], radius / 1000.0, bearing1, bearing2, {
    steps,
    units: 'kilometers'
  })
  ret.properties = properties
  return ret as Feature<Polygon, P>
}
